//
//  RegistViewController.m
//  WebSocketDemo
//
//  Created by 鲁征东 on 2020/6/2.
//  Copyright © 2020 newbike. All rights reserved.
//

#import "RegistViewController.h"

@interface RegistViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *accountTf;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (weak, nonatomic) IBOutlet UIImageView *headerImgV;

@property (nonatomic,strong) NSMutableDictionary  *paramer;
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];



}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (IBAction)registBtnclik:(id)sender {
    
    [self.view endEditing:YES];
    
    
    if (_accountTf.text.length==0 || _passWord.text.length==0) {
        return;
    }
    if ([_paramer[@"avatar"] length]==0) {
        return;
    }
    
    [self.paramer setValue:_accountTf.text forKey:@"username"];
    [self.paramer setValue:_passWord.text forKey:@"password"];

    
    
    [LZDSocketTool PostRegistWithParamer:_paramer Result:^(BaseModel * _Nonnull baseModel) {
       
        if (baseModel&& baseModel.code==0) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
   
    
    
}
- (IBAction)tapClick:(UITapGestureRecognizer *)sender {
    
    [self selectImageSource];
    
 
}


-(void)selectImageSource{
    
    
    UIImagePickerController *imagePick = [[UIImagePickerController alloc]init];
    imagePick.delegate = self;
    imagePick.allowsEditing = YES;
    
    
    UIAlertController *sheet = [UIAlertController alertControllerWithTitle:@"选择图片" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
     
     UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
         imagePick.sourceType =  UIImagePickerControllerSourceTypePhotoLibrary;
         [self presentViewController:imagePick animated:YES completion:nil];
         
         
     }];
     UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

         if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
             
             imagePick.sourceType = UIImagePickerControllerSourceTypeCamera;
             [self presentViewController:imagePick animated:YES completion:nil];

         }else{
             NSLog(@"相机不可用");
         }
         
         
         
         
     }];
     UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
     
     
     [sheet addAction:action1];
     [sheet addAction:action2];
     [sheet addAction:action3];

     
     [self presentViewController:sheet animated:YES completion:nil];
     
    
    
    
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
     [picker dismissViewControllerAnimated:YES completion:nil];
    
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.headerImgV.image = image;
    
    
    [SVProgressHUD show];
    
    
    [LZDSocketTool UpLoadImage:image Type:@"" Result:^(NSString * _Nonnull remoteFilePath) {

         [SVProgressHUD dismiss];
        
        [self.paramer setValue:remoteFilePath forKey:@"avatar"];

        
    }];
    

    
    
    
    
}



-(NSMutableDictionary *)paramer{
    if (!_paramer) {
        _paramer = [NSMutableDictionary dictionary];
    }
    return _paramer;
}
@end
